/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.diplomka.datalayer.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Class for connection to database
 *
 * @author Jakub Kutil
 */
public class DbConnector {

    /**
     * Connects to DB
     *
     * @return DB connection
     */
    public static Connection getDBConnection() {
        String url = "jdbc:postgresql://localhost:5433/Diplomka";
        String user = "postgres";
        String password = "test";

        Connection dbConnection = null;

        try {
            Class.forName("org.postgresql.Driver");
        } catch (ClassNotFoundException e) {
            System.out.println(e.getMessage());
        }

        try {
            dbConnection = DriverManager.getConnection(url, user, password);
            return dbConnection;
        } catch (SQLException e) {
            System.out.println(e.getMessage());
        }
        return dbConnection;
    }

    /**
     * Closes current connection
     *
     * @param rs result set to close
     * @param ps prepared statement to close
     * @param con connection to close
     */
    public static void closeConnection(ResultSet rs, PreparedStatement ps, Connection con) {
        try {
            if (rs != null) {
                rs.close();
            }
        } catch (Exception e) { /* ignored */ }

        try {
            if (ps != null) {
                ps.close();
            }
        } catch (Exception e) { /* ignored */ }
        try {
            if (con != null) {
                con.close();
            }
        } catch (Exception e) { /* ignored */ }
    }
}
